package com.yorge.controller;

import com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;
import java.util.function.Function;

@RestController
public class TestController {

    @GetMapping(value = "/hello")
    @SentinelResource("hello")
    public String hello() {
        return "Hello Sentinel";
    }

    @GetMapping(value = "/testA")
    @SentinelResource("testA")
    public String testA() throws Exception {
        return "Hello testA";
    }

    @GetMapping(value = "/testB")
    public String testB() throws InterruptedException {
        return "Hello testB";
    }

/*    @GetMapping("/mono")
    @SentinelResource(value = "mono", blockHandler = "monoBak")
    public Mono<String> mono() throws InterruptedException {
        Function<? super Mono<String>, ? extends Publisher<String>> transformer;
        TimeUnit.SECONDS.sleep(25);
        return Mono.just("simple string").transform(new SentinelReactorTransformer<>("otherResourceName"));
    }
    // blockHandler 在原方法调用被限流/降级/系统保护的时候调用, 而 fallback 函数会针对所有类型的异常
    public Mono<String> monoBak(BlockException blockException) {
        System.out.println("异常");

        return Mono.empty();
    }*/
}
